<?php
class Hunt {
	var $id = 0;
	var $deadline = false;
	var $linkid = 0;
	var $userid = 0; //indeed this item is not used yet and maybe will not be used
	var $stage = 'discard';
	var $private_info = '';

	function store() {
		global $db, $current_user;

		if(!$this->deadline) $this->deadline=time();
		$hunt_stage = $this->stage;
		$hunt_id = $this->id;
		$hunt_deadline = $this->deadline;
		$hunt_link_id = $this->linkid;
		$hunt_user_id = $this->userid;
		$hunt_private_info = $this->private_info;
		
	
		if($this->id===0) {
			$sql = "INSERT INTO " . table_hunts . " (hunt_link_id, hunt_user_id, hunt_stage, hunt_deadline, hunt_private_info) VALUES ($hunt_link_id,$hunt_user_id,'$hunt_stage', FROM_UNIXTIME($hunt_deadline), '$hunt_private_info')";
			if($this->debug == true){echo '<hr>store_basic:Insert:' . $sql . '<hr>';}
			$db->query($sql);
			$this->id = $db->insert_id;
		} else {
		// update
			$sql = "UPDATE " . table_hunts . " set `hunt_link_id`=$hunt_link_id, `hunt_user_id`=$hunt_user_id, hunt_stage='$hunt_stage', hunt_deadline=FROM_UNIXTIME($hunt_deadline), hunt_private_info='$hunt_private_info' WHERE hunt_id=$this->id";
			if($this->debug == true){echo '<hr>store_basic:Update:' . $sql . '<hr>';}
			$db->query($sql);
		}
	}

	function read($usecache = TRUE) {
		global $db, $current_user, $cached_hunts;
		$id = $this->id;
		
		if ($id == 0) {
			//get $id
			if ($this->linkid > 0) {
				$this->id = $db->get_var("SELECT hunt_id FROM " . table_hunts . " WHERE hunt_link_id=" . $this->linkid);
				$id = $this->id;
			}
			else {
				return false;
			}
		}

		// check to see if the link is cached
		// if it is, use it
		// if not, get from mysql and save to cache
		
		if (isset($cached_hunts[$id]) && $usecache == TRUE) {
			$hunt = $cached_hunts[$id];
		} else {
			$hunt = $db->get_row("SELECT " . table_hunts . ".* FROM " . table_hunts . " WHERE hunt_id = $id");
			$cached_hunts[$id] = $hunt;
		}
    
		if($hunt) {
			$hunt_stage = $hunt->hunt_stage;
			$hunt_link_id = $hunt->hunt_link_id;
			$hunt_user_id = $hunt->hunt_user_id;
			$hunt_deadline=$hunt->hunt_deadline;
			$hunt_private_info = $hunt->hunt_private_info;
			$this->stage = $hunt_stage;
			$this->linkid = $hunt_link_id;
			$this->userid = $hunt_user_id;
			$this->deadline=unixtimestamp($hunt_deadline);
			$this->private_info = $hunt_private_info;
			return true;
		}
		return false;
	}

	function add_apply($applyuserid) {
		global $db;
		if (!($applyuserid > 0)) {
			return false;
		}
		if ($this->id == 0) {
			$this->read();
		}
		//return "INSERT INTO " . table_apply . " ( apply_link_id,apply_hunt_id,apply_user_id,apply_stage) VALUES (0,$this->id,$applyuserid,'applied')";
		$db->query("INSERT INTO " . table_apply . "( apply_link_id,apply_hunt_id,apply_user_id,apply_stage) VALUES (0,$this->id,$applyuserid,'applied')");
		$appid = $db->insert_id;
		if ($appid) {
			require_once(mnminclude . "link.php");
			$applylink = new Link();
			$applylink->url = "hunt.php?appid=" . $appid;
			$applylink->randkey = rand(500,50000);
			$applylink->valid = true;
			$applylink->category = get_category_id("apply");
			$applylink->status='published';
			$applylink->published_date = time();
			$applylink->author=$applyuserid;
			$applylink->title = '认领';
			$applylink->title_url = makeUrlFriendly($applylink->title);
			$applylink->content = strip_tags('认领', Story_Content_Tags_To_Allow);
			$applylink->content = str_replace("\n", "<br />", $applylink->content);
			$applylink->link_summary = utf8_substr(strip_tags(trim('认领'), Story_Content_Tags_To_Allow), 0, StorySummary_ContentTruncate - 1);
			$applylink->link_summary = str_replace("\n", "<br />", $applylink->link_summary);
			
			$applylink->store();
			totals_adjust_count('published', 1);
			
			$applylink->read(false);
			$db->query("UPDATE " . table_apply . " SET apply_link_id=" . $applylink->id . " WHERE apply_id=" . $appid);
			$db->query("INSERT INTO " . table_link_owner . " (link_id,owner_id,percent) VALUES ($applylink->id,$applyuserid,100)");
			$applylink = '';
		}
		
		return true;
		
	}
}

?>
